layui.use(['layer', 'form', 'admin', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#uploadImg'
        ,url: Feng.ctxPath + '/upload/uploadImg'
        ,accept: 'images' //普通文件
        ,acceptMime: 'images/*'
        ,size: '102400'
        ,exts:'jpg|jpeg|png|gif|bmp'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#imgShow').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败' + res.msg);
            }
            //上传成功
            $(".imageClass").val(res.img_path);
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#imgText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //裁切事件
    $('#cutImage').click(function () {
        var imageClass = $(".imageClass").val();
        var width = $("#width").val();
        var height = $("#height").val();

        if(imageClass == ""){
            Feng.error("请先上传图片");
            return false;
        }
        if(width == "" || height == ""){
            Feng.error("请输入裁切比例");
            return false;
        }

        var img = new Image();
        img.src = imageClass+"?t="+new Date().getTime();
        var natureWitdh;
        var natureHeight;
        // 完全加载完毕的事件
        img.onload = function(){
            natureWitdh = img.width+150;
            natureHeight = img.height+150;
            top.layui.admin.open({
                type: 2,
                title: '裁切',
                area:[natureWitdh+"px",natureHeight+"px"],
                maxmin:true,
                content: Feng.ctxPath + '/upload/toCut?imgPath='+encodeURI(imageClass)+"&width="+width+"&height="+height,
                end: function () {
                    setTimeout(function () {
                        $("#imgShow").attr("src",imageClass+"?tm="+ new Date().getTime());
                    },500)
                }
            });
        }
    });

});